package com.example.demo.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.security.Principal;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello(Principal principal) {
//        return "Hello," + principal.getName();
        return "Hello,aa";
    }

    @GetMapping("/code")
    public ResponseEntity<String> code(String code) {
//        return "Hello," + principal.getName();
        System.out.println(code);
        RestTemplate restTemplate = new RestTemplate();
        String access_token_url = "http://localhost:8080/oauth/token";

        MultiValueMap<String, Object> hashMap = new LinkedMultiValueMap<>();
        hashMap.add("grant_type","authorization_code");
        hashMap.add("code",code);
        hashMap.add("client_id","along");
        hashMap.add("client_secret","code");
        hashMap.add("redirect_uri","http://127.0.0.1:8081/code");

        ResponseEntity<String> responseEntity = restTemplate.postForEntity(access_token_url, hashMap, String.class);
        return responseEntity;
    }

}
